using System;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.Security;
namespace HKB.Common
{
	public class Text
	{
		public static string Md5(string str)
		{
			return FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").ToLower();
		}
		public static string Md5(string str, int n)
		{
			string text = FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").ToLower();
			string result;
			if (n == 32)
			{
				result = text;
			}
			else
			{
				if (n == 16)
				{
					result = text.Substring(8, 16);
				}
				else
				{
					result = null;
				}
			}
			return result;
		}
		public static string HTMLEncode(string str)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder(str);
			stringBuilder.Replace("&", "&amp;");
			stringBuilder.Replace("<", "&lt;");
			stringBuilder.Replace(">", "&gt;");
			stringBuilder.Replace("\"", "&quot;");
			stringBuilder.Replace("'", "&#39;");
			stringBuilder.Replace("\t", "&nbsp; &nbsp; ");
			stringBuilder.Replace("\r", "");
			stringBuilder.Replace("\n", "<br />");
			return stringBuilder.ToString();
		}
		public static string MakeOutTradeNo()
		{
			System.Random random = new System.Random();
			return (10 + random.Next(9999999)).ToString();
		}
		public static string MakePassword()
		{
			string text = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
			string text2 = "";
			int num = 8;
			System.Random random = new System.Random();
			for (int i = 0; i < num; i++)
			{
				int index = random.Next(text.Length);
				text2 += text[index];
			}
			return text2;
		}
		public static string MakePassword(int pwdlen)
		{
			string text = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
			string text2 = "";
			System.Random random = new System.Random();
			for (int i = 0; i < pwdlen; i++)
			{
				int index = random.Next(text.Length);
				text2 += text[index];
			}
			return text2;
		}
		public static string Random(int strlen, int strtype)
		{
			string text = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
			if (strtype == 0)
			{
				text = "0123456789";
			}
			else
			{
				if (strtype == 1)
				{
					text = "abcdefghijklmnopqrstuvwxyz";
				}
				else
				{
					if (strtype == 2)
					{
						text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
					}
					else
					{
						if (strtype == 3)
						{
							text = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
						}
					}
				}
			}
			string text2 = "";
			System.Random random = new System.Random();
			for (int i = 0; i < strlen; i++)
			{
				int index = random.Next(text.Length);
				text2 += text[index];
			}
			return text2;
		}
		public static bool ProcessSqlStr(string Str)
		{
			bool result = true;
			try
			{
				if (Str != "")
				{
					string text = "exec|insert|select|delete|update|count|*|chr|mid|master|truncate|char|declare|'|;";
					string[] array = text.Split(new char[]
					{
						'|'
					});
					string[] array2 = array;
					for (int i = 0; i < array2.Length; i++)
					{
						string value = array2[i];
						if (Str.IndexOf(value) >= 0)
						{
							result = false;
						}
					}
				}
			}
			catch
			{
				result = false;
			}
			return result;
		}
		public static string HtmlReplace(string Str)
		{
			return Regex.Replace(Str, "<w>", "");
		}
		public static string ReplaceBadChar(string strChar)
		{
			string text = "";
			string result;
			if (strChar == "" || string.IsNullOrEmpty(strChar))
			{
				result = text;
			}
			else
			{
				strChar = Text.HTMLEncode(strChar);
				result = strChar;
			}
			return result;
		}
		public static bool CheckEmail(string email)
		{
			return !(email == string.Empty) && email.Length <= 60 && email.Contains("@") && email.IndexOf("@") + 1 <= email.Length && email.Substring(email.IndexOf("@") + 1).Contains(".");
		}
		public static bool CheckNameLength(string Name, int Min, int Max)
		{
			bool result;
			if (Name != "")
			{
				int strLen = Text.GetStrLen(Name);
				result = (strLen <= Max && strLen >= Min);
			}
			else
			{
				result = false;
			}
			return result;
		}
		public static int GetStrLen(string ss)
		{
			string arg = "";
			char[] array = ss.ToCharArray();
			int num = ss.Length;
			int result;
			if ("恒浪".Length == 4)
			{
				result = num;
			}
			else
			{
				for (int i = 0; i < array.Length; i++)
				{
					if (System.Convert.ToInt32(array[i]) > 255)
					{
						num++;
						arg += array[i];
					}
				}
				result = num;
			}
			return result;
		}
		public static string CheckNumList(string str)
		{
			string result;
			if (str == "" || str == null)
			{
				result = string.Empty;
			}
			else
			{
				if (str.Contains(","))
				{
					string[] array = str.Split(new char[]
					{
						','
					});
					for (int i = 0; i < array.Length; i++)
					{
						if (array[i] == "")
						{
							result = string.Empty;
							return result;
						}
						try
						{
							int.Parse(array[i]);
						}
						catch
						{
							result = string.Empty;
							return result;
						}
					}
				}
				else
				{
					try
					{
						int.Parse(str);
					}
					catch
					{
						result = string.Empty;
						return result;
					}
				}
				result = str;
			}
			return result;
		}
		public static bool IsNum(string str)
		{
			bool result;
			for (int i = 0; i < str.Length; i++)
			{
				if (str[i] < '0' || str[i] > '9')
				{
					result = false;
					return result;
				}
			}
			result = true;
			return result;
		}
		public static int ToInt32(string str)
		{
			int num = 0;
			int result;
			try
			{
				num = System.Convert.ToInt32(str);
			}
			catch
			{
				result = 0;
				return result;
			}
			result = num;
			return result;
		}
		public static int ToInt32(object str)
		{
			int num = 0;
			int result;
			try
			{
				num = System.Convert.ToInt32(str);
			}
			catch
			{
				result = 0;
				return result;
			}
			result = num;
			return result;
		}
		private static bool IsEmail(string s)
		{
			string pattern = "^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$";
			return Regex.IsMatch(s, pattern);
		}
		public static bool IsNumber(string strNumber)
		{
			bool result;
			if (!string.IsNullOrEmpty(strNumber))
			{
				try
				{
					double.Parse(strNumber);
				}
				catch
				{
					result = false;
					return result;
				}
				result = true;
			}
			else
			{
				result = false;
			}
			return result;
		}
		public static bool CheckNumList(string str, char separator)
		{
			bool result;
			if (null == str)
			{
				result = false;
			}
			else
			{
				int num = str.Length;
				if (0 == num)
				{
					result = false;
				}
				else
				{
					if (!char.IsNumber(str[0]) || !char.IsNumber(str[num - 1]))
					{
						result = false;
					}
					else
					{
						num--;
						for (int i = 1; i < num; i++)
						{
							if (separator == str[i])
							{
								if (!char.IsNumber(str[i - 1]) || !char.IsNumber(str[i + 1]))
								{
									result = false;
									return result;
								}
							}
							else
							{
								if (!char.IsNumber(str[i]))
								{
									result = false;
									return result;
								}
							}
						}
						result = true;
					}
				}
			}
			return result;
		}
		public static decimal ToDecimal(string str)
		{
			decimal result;
			if (Text.IsNumber(str))
			{
				result = System.Convert.ToDecimal(str);
			}
			else
			{
				result = 0m;
			}
			return result;
		}
		public static decimal ToDecimal(object str)
		{
			decimal result = 0m;
			try
			{
				result = System.Convert.ToDecimal(str);
			}
			catch
			{
			}
			return result;
		}
		public static bool IsDate(string value)
		{
			bool result;
			try
			{
				System.DateTime.Parse(value);
			}
			catch
			{
				result = false;
				return result;
			}
			result = true;
			return result;
		}
		public static bool CheckChineaseName(string StrName)
		{
			return Regex.IsMatch(StrName, "[一-龥豈-鶴]");
		}
		public static bool CheckMobilePhone(string str)
		{
			return !(str == string.Empty) && str.Length <= 12;
		}
		public static bool CheckMd5Text(string str, string cstr)
		{
			bool result;
			if (str == string.Empty || cstr == string.Empty)
			{
				result = false;
			}
			else
			{
				str = str.ToLower();
				cstr = cstr.ToLower();
				if (str == cstr)
				{
					result = true;
				}
				else
				{
					if (str.Length == 32)
					{
						str = str.Substring(0, 16);
					}
					if (cstr.Length == 32)
					{
						cstr = cstr.Substring(0, 16);
					}
					result = (str == cstr);
				}
			}
			return result;
		}
		public static string GetComeUrl(string comeurl)
		{
			string result;
			if (comeurl == string.Empty)
			{
				result = string.Empty;
			}
			else
			{
				if (comeurl.Contains("---"))
				{
					comeurl = comeurl.Replace("---", "&");
				}
				result = comeurl;
			}
			return result;
		}
		public static string wipeScript(string html)
		{
			if (html != null && html != "")
			{
				Regex regex = new Regex("<script[\\s\\S]+</script *>", RegexOptions.IgnoreCase);
				Regex regex2 = new Regex(" href\\s*=.*javascript[^>\\s]*", RegexOptions.IgnoreCase);
				Regex regex3 = new Regex(" on\\w+=", RegexOptions.IgnoreCase);
				Regex regex4 = new Regex("<iframe[\\s\\S]+</iframe *>", RegexOptions.IgnoreCase);
				Regex regex5 = new Regex("<frameset[\\s\\S]+</frameset *>", RegexOptions.IgnoreCase);
				Regex regex6 = new Regex("<input[\\s\\S]+</input *>", RegexOptions.IgnoreCase);
				Regex regex7 = new Regex("<style[\\s\\S]+</style *>", RegexOptions.IgnoreCase);
				Regex regex8 = new Regex("<link[\\s\\S]+</link *>", RegexOptions.IgnoreCase);
				Regex regex9 = new Regex("<(input|link|iframe|frameset|frame)[^>]*/>", RegexOptions.IgnoreCase);
				Regex regex10 = new Regex("<form[\\s\\S]+</form *>", RegexOptions.IgnoreCase);
				html = regex.Replace(html, string.Empty);
				html = regex6.Replace(html, string.Empty);
				html = regex3.Replace(html, " _disibledevent=");
				html = regex4.Replace(html, string.Empty);
				html = regex5.Replace(html, string.Empty);
				html = regex7.Replace(html, string.Empty);
				html = regex8.Replace(html, string.Empty);
				html = regex9.Replace(html, string.Empty);
				html = regex2.Replace(html, " href=\"#\"");
				html = regex10.Replace(html, string.Empty);
			}
			return html;
		}
		public static string SQLEncode(string Str)
		{
			return Str.Trim().Replace("'", "''");
		}
		public static string GetUserBalanceKey(int uid, decimal balance)
		{
			return Text.Md5(string.Concat(new object[]
			{
				uid,
				"_",
				balance,
				"_",
				AppSetings.Get("WebKey")
			}), 32);
		}
		public static T GetComponent<T>(string name, object element)
		{
			System.Type type = element.GetType();
			return (T)((object)type.InvokeMember(name, System.Reflection.BindingFlags.IgnoreCase | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.GetField | System.Reflection.BindingFlags.GetProperty, null, element, null));
		}
		public static string GBToUnicode(string text)
		{
			byte[] bytes = System.Text.Encoding.Unicode.GetBytes(text);
			string text2 = "";
			string text3 = "";
			for (int i = 0; i < bytes.Length; i++)
			{
				if (i % 2 == 0)
				{
					text3 = System.Convert.ToString(bytes[i], 16);
					if (text3.Length < 2)
					{
						text3 = "0" + text3;
					}
				}
				else
				{
					string text4 = System.Convert.ToString(bytes[i], 16);
					if (text4.Length < 2)
					{
						text4 = "0" + text4;
					}
					text2 = text2 + "\\u" + text4 + text3;
				}
			}
			return text2;
		}
	}
}
